package com.teamawesome.hackathon;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    private String[] title;
    private String[] price;
    
    /**
     * Constructor for the Lazy Adapter
     * 
     * @param a
     * @param imgs = image string [] array
     * @param title = title string [] array
     */
    public LazyAdapter(Activity a, String[] imgs, String[] title, String[] price) {
        activity = a;
        data=imgs;
        this.title = title;
        this.price = price;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return title.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        TextView text=(TextView)vi.findViewById(R.id.text);
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        text.setText(title[position]);
        try{
            imageLoader.DisplayImage(data[position], activity, image);
        } catch (Exception e){
        }
        TextView subText = (TextView)vi.findViewById(R.id.subtext);

        String priceString = (price != null && price[position] != null) ? price[position] : "No price given.";
        subText.setText(priceString);

        return vi;
    }
}